package com.hnchances.wjb.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnchances.wjb.Dto.UserDto;
import com.hnchances.wjb.Service.StudentService;
import com.hnchances.wjb.Service.TeacherService;
import com.hnchances.wjb.Service.UserService;
import com.hnchances.wjb.common.R;
import com.hnchances.wjb.entity.Student;
import com.hnchances.wjb.entity.Teacher;
import com.hnchances.wjb.entity.User;
import com.hnchances.wjb.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    StudentService studentService;
    @Autowired
    UserService userService;
    @Autowired
    TeacherService teacherService;
@Autowired
UserMapper userMapper;
    @Override
//    @Transactional
    public R<String> register(UserDto userDto) {
        User user = new User();
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();

        log.info("ishidahdi");
        //根据课程id 判断学生还是老师注册
        if (userDto.getCourseID()!=null&&!"".equals(userDto.getCourseID())){
            LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Teacher::getClassID,userDto.getClassID());
            lambdaQueryWrapper.eq(Teacher::getCourseID,userDto.getCourseID());
            lambdaQueryWrapper.eq(Teacher::getName,userDto.getTrueName());
            Teacher teacher = teacherService.getOne(lambdaQueryWrapper);
            if (teacher!=null){
                BeanUtils.copyProperties(userDto,user);
                System.out.println(user);
                String password = user.getPassWord();
                password = DigestUtils.md5DigestAsHex(password.getBytes());
                user.setPassWord(password);
                user.setTrueName(userDto.getTrueName());
                user.setUserName(user.getUserName());
                user.setCreatDateTime(LocalDateTime.now());
                user.setUpdateByID(null);
                user.setIdentity(1);
                user.setLastLoginDateTime(LocalDateTime.now());
                user.setLoginIP("ipv4");
                user.setStatus(0);
                user.setVerify(0);
                userService.save(user);
                System.out.println(user);
                //将新注册用户的Id填入关系表中

                saveUser_rolr(userDto,2);
               /* userLambdaQueryWrapper.eq(User::getUserName,userDto.getUserName());
//                userLambdaQueryWrapper.eq(User::getCourseID,userDto.getCourseID());
                user = userService.getOne(userLambdaQueryWrapper);
                System.out.println(user);*/
//                userMapper.insertRoles(user.getId(),2);
                return R.success("老师注册成功");
            }
            return R.error("注册失败，该课程班级中，查不到老师的信息",400);

        }else {
            LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Student::getIdcard,userDto.getIdcard());
            lambdaQueryWrapper.eq(Student::getName,userDto.getTrueName());
            lambdaQueryWrapper.eq(Student::getClassID,userDto.getClassID());
            Student student = studentService.getOne(lambdaQueryWrapper);
            if (student!=null){
                BeanUtils.copyProperties(userDto,user);
                String password = userDto.getPassWord();
                password = DigestUtils.md5DigestAsHex(password.getBytes());
                user.setPassWord(password);
                user.setTrueName(userDto.getTrueName());
                user.setUserName(user.getUserName());
                user.setIdentity(0);
                user.setCreatDateTime(LocalDateTime.now());
                user.setUpdateByID(null);
                user.setLastLoginDateTime(LocalDateTime.now());
                user.setLoginIP("ipv4");
                user.setStudentID(student.getId());
                user.setClassID(userDto.getClassID());
                user.setStatus(0);
                user.setVerify(0);
                userService.save(user);
                //将新注册用户的Id填入关系表中
                saveUser_rolr(userDto,1);
                return R.success("学生注册成功");
            }
            return R.error("注册失败！该班级中未查到此学生",400);
        }
    }
   public  int saveUser_rolr(UserDto userDto,int role){
        User user = new User();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String password = userDto.getPassWord();
        lambdaQueryWrapper.eq(User::getPassWord, DigestUtils.md5DigestAsHex(password.getBytes()));
        lambdaQueryWrapper.eq(User::getUserName,userDto.getUserName());
        user = userService.getOne(lambdaQueryWrapper);
        userMapper.insertRoles(user.getId(),role);
        return 0;
    }
}
